/**
 * @Title: UserModal.js
 * @Description: UserModal
 * @author: Fanchao
 * @date: 2020/8/2 11:12 上午
 * @version V1.0
 */

import React, { memo, useEffect, useState } from "react";
import { connect } from "umi";
import { DatePicker, Form, Input, Modal, Select, InputNumber, message } from "antd";
import moment from "moment";
import { getRunTrainTaskInfoList, getRunCourseList, getTrainingTypeList, getTeachClassList } from "api/index";

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 18 }
};
const { Option } = Select;
const { RangePicker } = DatePicker;

const UserModal = memo(({ theObjects: { itemData, teacherOptions }, dispatch, title, visible }) => {
    const [exemptBasisShow, setExemptBasisShow] = useState(false);
    const [evaluateShow, setEvaluateShow] = useState(false);
    const [trainingTypeList, setTrainingTypeList] = useState([]);
    const [runTrainTaskInfoList, setRunTrainTaskInfoList] = useState([]);
    const [courseListOptions, setCourseListOptions] = useState([]);
    const [teachClassList, setTeachClassList] = useState([]);
    const [form] = Form.useForm();
    const checkMin = (rule, value, callback) => {
        if (value < 20) {
            callback("最少20");
        } else if (!value) {
            callback("必填项");
        } else {
            callback();
        }
    };
    const fetchRunTrainTaskInfoList = async (filter) => {
        const result = await getRunTrainTaskInfoList({ trainingTypeId: filter });
        if (result.status === "success") {
            const runTrainTaskInfoList = result.result;
            setRunTrainTaskInfoList(runTrainTaskInfoList);
        } else {
            message.error(result.message);
        }
    };

    const fetchTrainingTypeList = async () => {
        const result = await getTrainingTypeList();
        if (result.status === "success") {
            const trainingTypeList = result.result;
            setTrainingTypeList(trainingTypeList);
        } else {
            message.error(result.message);
        }
    };
    const fetchCourseListOptions = async (majorDirectionId) => {
        const result = await getRunCourseList({ majorDirectionId });
        if (result.status === "success") {
            const courseListOptions = result.result;
            setCourseListOptions(courseListOptions);
        } else {
            message.error(result.message);
        }
    };
    const fetchTeachClassList = async () => {
        const result = await getTeachClassList();
        if (result.status === "success") {
            const teachClassList = result.result;
            setTeachClassList(teachClassList);
        } else {
            message.error(result.message);
        }
    };
    useEffect(() => {
        fetchTrainingTypeList();
        fetchTeachClassList();
    }, []);

    return (
        <Modal
            title={title}
            visible={visible}
            width="900px"
            onOk={() => {
                form.validateFields().then(values => {
                    if (itemData.evaluateObjectId) {
                        values.evaluateObjectId = itemData.evaluateObjectId;
                        dispatch({
                            type: "theObjects/updateEvaluateObjectInfo",
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: "theObjects/setItemData",
                                        payload: {}
                                    });
                                    dispatch({
                                        type: "theObjects/setModalVisible",
                                        payload: false
                                    });
                                    dispatch({
                                        type: "theObjects/getDataSource",
                                        payload: {
                                            keyword: "",
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    } else {
                        values.schoolYearStart = moment(values.time[0]).format("YYYY-MM-DD");
                        values.schoolYearEnd = moment(values.time[1]).format("YYYY-MM-DD");
                        values.exemptEvaluate = values.exemptEvaluate === "1" ? true : false;
                        values.smallClass = values.smallClass === "1" ? true : false;
                        values.newTeacher = values.newTeacher === "1" ? true : false;
                        values.evaluate = values.evaluate === "1" ? true : false;
                        delete values.time;
                        dispatch({
                            type: "theObjects/addEvaluateObject",
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: "theObjects/setItemData",
                                        payload: {}
                                    });
                                    dispatch({
                                        type: "theObjects/setModalVisible",
                                        payload: false
                                    });
                                    dispatch({
                                        type: "theObjects/getDataSource",
                                        payload: {
                                            keyword: "",
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                dispatch({
                    type: "theObjects/setItemData",
                    payload: {}
                });
                dispatch({
                    type: "theObjects/setModalVisible",
                    payload: false
                });
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    exemptEvaluate: "0",
                    smallClass: "0",
                    newTeacher: "0"
                }}
            >


                <Form.Item
                    label="培训类型"
                    name="trainingTypeId"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select
                        placeholder="培训类型"
                        onChange={(e) => {
                            fetchRunTrainTaskInfoList(e);
                        }}
                    >
                        {
                            trainingTypeList.map(item => {
                                return <Option key={item.trainTypeId}
                                               value={item.trainTypeId}>{item.trainingTypeName}</Option>;
                            })
                        }
                    </Select>
                </Form.Item>
                <Form.Item
                    label="任务运行"
                    name="runTrainTaskId"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select
                        placeholder="任务运行"
                        onChange={(item) => {
                            fetchCourseListOptions(item.majorDirectionId);
                        }}
                    >
                        {
                            runTrainTaskInfoList.map(item => {
                                return <Option key={item.runTrainTaskId}
                                               value={item.runTrainTaskId}>{item.majorSysCode} {item.majorName} {item.startDate}至{item.endDate}</Option>;
                            })
                        }
                    </Select>
                </Form.Item>
                <Form.Item
                    label="请选择运行课程"
                    name="runCourseId"
                    rules={[{ required: true, message: "必填" }]}
                >
                    <Select
                        placeholder="请选择运行课程"
                    >
                        {
                            courseListOptions.map(i => {
                                return <Option
                                    value={i.runCourseId}
                                    key={i.runCourseId}
                                >
                                    {`${i.courseName}`+`_`+`${i.schoolYear}`+`_`+`${i.semester}`}
                                </Option>;
                            })
                        }
                    </Select>
                </Form.Item>

                {
                    itemData.evaluateObjectId ? null : (
                        <>
                            <Form.Item
                                label="教员"
                                name="teacherUserId"
                                rules={[{ required: true, message: "必填" }]}
                            >
                                <Select
                                    placeholder="请选择教员"
                                >
                                    {
                                        teacherOptions.map(item => {
                                            return (
                                                <Option
                                                    value={item.teacherUserId}
                                                    key={item.teacherUserId}
                                                >
                                                    {`${item.teacherRealName}` +'_'+`${item.department}`}
                                                </Option>
                                            );
                                        })
                                    }
                                </Select>
                            </Form.Item>

                            <Form.Item
                                required
                                label="上课学时"
                                name="actualClassHours"
                                rules={[
                                    { validator: checkMin }
                                ]}
                            >
                                <InputNumber style={{ width: "100%" }} placeholder="请输入实际上课学时"/>
                            </Form.Item>

                            <Form.Item
                                label="免评对象"
                                name="exemptEvaluate"
                                rules={[{ required: true, message: "必填" }]}
                            >
                                <Select
                                    placeholder="是否为免评对象"
                                    onChange={(e) => {
                                        setExemptBasisShow(e === "1" ? true : false);
                                    }}
                                >
                                    <Option value="1">是</Option>
                                    <Option value="0">否</Option>
                                </Select>
                            </Form.Item>
                            {
                                exemptBasisShow ? (
                                    <Form.Item
                                        label="免评依据"
                                        name="exemptBasis"
                                        rules={[{ required: false, message: "必填" }]}
                                    >
                                        <Select
                                            placeholder="选择免评依据"
                                        >
                                            <Option value="专家免评">专家免评</Option>
                                            <Option value="连优免评">连优免评</Option>
                                            <Option value="督导免评">督导免评</Option>
                                        </Select>
                                    </Form.Item>
                                ) : null
                            }
                            <Form.Item
                                label="小班教学"
                                name="smallClass"
                                rules={[{ required: true, message: "必填" }]}
                            >
                                <Select
                                    placeholder="是否小班"
                                >
                                    <Option value="1">是</Option>
                                    <Option value="0">否</Option>
                                </Select>
                            </Form.Item>
                            <Form.Item
                                label="新教员上课"
                                name="newTeacher"
                                rules={[{ required: true, message: "必填" }]}
                            >
                                <Select
                                    placeholder="是否新教员上课"
                                    onChange={(e) => {
                                        setEvaluateShow(e === "1" ? true : false);
                                    }}
                                >
                                    <Option value="1">是</Option>
                                    <Option value="0">否</Option>
                                </Select>
                            </Form.Item>
                            {
                                evaluateShow ? (
                                    <Form.Item
                                        label="是否参评"
                                        name="evaluate"
                                        rules={[{ required: true, message: "必填" }]}
                                    >
                                        <Select
                                            placeholder="是否参评"
                                        >
                                            <Option value="1">是</Option>
                                            <Option value="0">否</Option>
                                        </Select>
                                    </Form.Item>
                                ) : null
                            }
                        </>
                    )
                }
            </Form>
        </Modal>
    );
});

export default connect(({ theObjects, dispatch }) => ({
    theObjects,
    dispatch
}))(UserModal);
